package cn.bjc.user.controller;

import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.user.domain.Logininfo;
import cn.bjc.user.query.LogininfoQuery;
import cn.bjc.user.service.ILogininfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
public class LogininfoController {
    
    @Autowired
    private ILogininfoService logininfoService;


    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有")
    public List<Logininfo> findAll() {
        return logininfoService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * url:http://localhost/logininfo/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public Logininfo findOne(@PathVariable("id") long id) {
        return logininfoService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/user/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            logininfoService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     *
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加操作接口")
    public JsonResult updateOrAdd(@RequestBody Logininfo logininfo){
        try {
            if(logininfo.getId()==null){
                //添加操作
                logininfoService.save(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query){
        try {
            return logininfoService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            logininfoService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    
    
}
